<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Introduction &mdash; ib_insync 0.9.70 documentation</title>
      <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
      <link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
    <link rel="canonical" href="https://ib_insync.readthedocs.ioreadme.html"/>
  
        <script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
        <script src="_static/jquery.js"></script>
        <script src="_static/underscore.js"></script>
        <script src="_static/doctools.js"></script>
    <script src="_static/js/theme.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="API docs" href="api.html" />
    <link rel="prev" title="Contents" href="index.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
            <a href="index.html" class="icon icon-home"> ib_insync
          </a>
              <div class="version">
                0.9
              </div>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <ul class="current">
<li class="toctree-l1 current"><a class="current reference internal" href="#">Introduction</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#installation">Installation</a></li>
<li class="toctree-l2"><a class="reference internal" href="#example">Example</a></li>
<li class="toctree-l2"><a class="reference internal" href="#documentation">Documentation</a></li>
<li class="toctree-l2"><a class="reference internal" href="#discussion">Discussion</a></li>
<li class="toctree-l2"><a class="reference internal" href="#consultancy-development">Consultancy &amp; Development</a></li>
<li class="toctree-l2"><a class="reference internal" href="#disclaimer">Disclaimer</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="api.html">API docs</a></li>
<li class="toctree-l1"><a class="reference internal" href="notebooks.html">Notebooks</a></li>
<li class="toctree-l1"><a class="reference internal" href="recipes.html">Code recipes</a></li>
<li class="toctree-l1"><a class="reference internal" href="code.html">Source code</a></li>
<li class="toctree-l1"><a class="reference internal" href="changelog.html">Changelog</a></li>
<li class="toctree-l1"><a class="reference internal" href="links.html">Links</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="index.html">ib_insync</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="index.html" class="icon icon-home"></a> &raquo;</li>
      <li>Introduction</li>
      <li class="wy-breadcrumbs-aside">
            <a href="_sources/readme.rst.txt" rel="nofollow"> View page source</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <p><a class="reference external" href="https://github.com/erdewit/ib_insync/actions"><img alt="Build" src="https://github.com/erdewit/ib_insync/actions/workflows/test.yml/badge.svg?branch=master" /></a> <a class="reference external" href="https://groups.io/g/insync"><img alt="Join the user group" src="https://img.shields.io/badge/groups.io-insync-green.svg" /></a> <img alt="" src="https://img.shields.io/badge/python-3.6+-blue.svg" /> <img alt="" src="https://img.shields.io/badge/status-beta-green.svg" /> <a class="reference external" href="https://pypi.python.org/pypi/ib_insync"><img alt="PyPi" src="https://img.shields.io/pypi/v/ib_insync.svg" /></a> <a class="reference external" href="https://anaconda.org/conda-forge/ib-insync"><img alt="Conda" src="https://img.shields.io/conda/vn/conda-forge/ib-insync.svg" /></a> <img alt="" src="https://img.shields.io/badge/license-BSD-blue.svg" /> <a class="reference external" href="https://pepy.tech/project/ib-insync"><img alt="Number of downloads" src="https://pepy.tech/badge/ib-insync" /></a> <a class="reference external" href="https://ib-insync.readthedocs.io/api.html"><img alt="Documentation" src="https://img.shields.io/badge/Documentation-green.svg" /></a></p>
<div class="section" id="introduction">
<h1>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline"></a></h1>
<p>The goal of the IB-insync library is to make working with the
<a class="reference external" href="http://interactivebrokers.github.io/tws-api/">Trader Workstation API</a>
from Interactive Brokers as easy as possible.</p>
<p>The main features are:</p>
<ul class="simple">
<li><p>An easy to use linear style of programming;</p></li>
<li><p>An <a class="reference external" href="https://ib-insync.readthedocs.io/api.html#module-ib_insync.ib">IB component</a>
that automatically keeps in sync with the TWS or IB Gateway application;</p></li>
<li><p>A fully asynchonous framework based on
<a class="reference external" href="https://docs.python.org/3/library/asyncio.html">asyncio</a>
and
<a class="reference external" href="https://github.com/erdewit/eventkit">eventkit</a>
for advanced users;</p></li>
<li><p>Interactive operation with live data in Jupyter notebooks.</p></li>
</ul>
<p>Be sure to take a look at the
<a class="reference external" href="https://ib-insync.readthedocs.io/notebooks.html">notebooks</a>,
the <a class="reference external" href="https://ib-insync.readthedocs.io/recipes.html">recipes</a>
and the <a class="reference external" href="https://ib-insync.readthedocs.io/api.html">API docs</a>.</p>
<div class="section" id="installation">
<h2>Installation<a class="headerlink" href="#installation" title="Permalink to this headline"></a></h2>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">pip</span> <span class="n">install</span> <span class="n">ib_insync</span>
</pre></div>
</div>
<p>For Python 3.6 install the <code class="docutils literal notranslate"><span class="pre">dataclasses</span></code> package as well
(newer Python versions already have it):</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">pip</span> <span class="n">install</span> <span class="n">dataclasses</span>
</pre></div>
</div>
<p>Requirements:</p>
<ul class="simple">
<li><p>Python 3.6 or higher;</p></li>
<li><p>A running TWS or IB Gateway application (version 972 or higher).
Make sure the
<a class="reference external" href="https://interactivebrokers.github.io/tws-api/initial_setup.html">API port is enabled</a>
and ‘Download open orders on connection’ is checked.</p></li>
</ul>
<p>The ibapi package from IB is not needed.</p>
</div>
<div class="section" id="example">
<h2>Example<a class="headerlink" href="#example" title="Permalink to this headline"></a></h2>
<p>This is a complete script to download historical data:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">ib_insync</span> <span class="kn">import</span> <span class="o">*</span>
<span class="c1"># util.startLoop()  # uncomment this line when in a notebook</span>

<span class="n">ib</span> <span class="o">=</span> <span class="n">IB</span><span class="p">()</span>
<span class="n">ib</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s1">&#39;127.0.0.1&#39;</span><span class="p">,</span> <span class="mi">7497</span><span class="p">,</span> <span class="n">clientId</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

<span class="n">contract</span> <span class="o">=</span> <span class="n">Forex</span><span class="p">(</span><span class="s1">&#39;EURUSD&#39;</span><span class="p">)</span>
<span class="n">bars</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqHistoricalData</span><span class="p">(</span>
    <span class="n">contract</span><span class="p">,</span> <span class="n">endDateTime</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">durationStr</span><span class="o">=</span><span class="s1">&#39;30 D&#39;</span><span class="p">,</span>
    <span class="n">barSizeSetting</span><span class="o">=</span><span class="s1">&#39;1 hour&#39;</span><span class="p">,</span> <span class="n">whatToShow</span><span class="o">=</span><span class="s1">&#39;MIDPOINT&#39;</span><span class="p">,</span> <span class="n">useRTH</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

<span class="c1"># convert to pandas dataframe:</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">util</span><span class="o">.</span><span class="n">df</span><span class="p">(</span><span class="n">bars</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">df</span><span class="p">)</span>
</pre></div>
</div>
<p>Output:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>                   <span class="n">date</span>      <span class="nb">open</span>      <span class="n">high</span>       <span class="n">low</span>     <span class="n">close</span>  <span class="n">volume</span>  \
<span class="mi">0</span>   <span class="mi">2019</span><span class="o">-</span><span class="mi">11</span><span class="o">-</span><span class="mi">19</span> <span class="mi">23</span><span class="p">:</span><span class="mi">15</span><span class="p">:</span><span class="mi">00</span>  <span class="mf">1.107875</span>  <span class="mf">1.108050</span>  <span class="mf">1.107725</span>  <span class="mf">1.107825</span>      <span class="o">-</span><span class="mi">1</span>
<span class="mi">1</span>   <span class="mi">2019</span><span class="o">-</span><span class="mi">11</span><span class="o">-</span><span class="mi">20</span> <span class="mi">00</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span>  <span class="mf">1.107825</span>  <span class="mf">1.107925</span>  <span class="mf">1.107675</span>  <span class="mf">1.107825</span>      <span class="o">-</span><span class="mi">1</span>
<span class="mi">2</span>   <span class="mi">2019</span><span class="o">-</span><span class="mi">11</span><span class="o">-</span><span class="mi">20</span> <span class="mi">01</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span>  <span class="mf">1.107825</span>  <span class="mf">1.107975</span>  <span class="mf">1.107675</span>  <span class="mf">1.107875</span>      <span class="o">-</span><span class="mi">1</span>
<span class="mi">3</span>   <span class="mi">2019</span><span class="o">-</span><span class="mi">11</span><span class="o">-</span><span class="mi">20</span> <span class="mi">02</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span>  <span class="mf">1.107875</span>  <span class="mf">1.107975</span>  <span class="mf">1.107025</span>  <span class="mf">1.107225</span>      <span class="o">-</span><span class="mi">1</span>
<span class="mi">4</span>   <span class="mi">2019</span><span class="o">-</span><span class="mi">11</span><span class="o">-</span><span class="mi">20</span> <span class="mi">03</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span>  <span class="mf">1.107225</span>  <span class="mf">1.107725</span>  <span class="mf">1.107025</span>  <span class="mf">1.107525</span>      <span class="o">-</span><span class="mi">1</span>
<span class="o">..</span>                  <span class="o">...</span>       <span class="o">...</span>       <span class="o">...</span>       <span class="o">...</span>       <span class="o">...</span>     <span class="o">...</span>
<span class="mi">705</span> <span class="mi">2020</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">02</span> <span class="mi">14</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span>  <span class="mf">1.119325</span>  <span class="mf">1.119675</span>  <span class="mf">1.119075</span>  <span class="mf">1.119225</span>      <span class="o">-</span><span class="mi">1</span>
</pre></div>
</div>
</div>
<div class="section" id="documentation">
<h2>Documentation<a class="headerlink" href="#documentation" title="Permalink to this headline"></a></h2>
<p>The complete <a class="reference external" href="https://ib-insync.readthedocs.io/api.html">API documentation</a>.</p>
<p><a class="reference external" href="https://ib-insync.readthedocs.io/changelog.html">Changelog</a>.</p>
</div>
<div class="section" id="discussion">
<h2>Discussion<a class="headerlink" href="#discussion" title="Permalink to this headline"></a></h2>
<p>The <a class="reference external" href="https://groups.io/g/insync">insync user group</a> is the place to discuss
IB-insync and anything related to it.</p>
</div>
<div class="section" id="consultancy-development">
<h2>Consultancy &amp; Development<a class="headerlink" href="#consultancy-development" title="Permalink to this headline"></a></h2>
<p>IB-insync offers an easy entry into building automated trading systems
for both individual traders and fintech companies. However, to get the most out
of it is not a trivial matter and is beyond the reach of most developers.</p>
<p>If you need expert help, you can contact me. This can be for a small project,
such as fixing something in your own code, or it can be creating an entire new
trading infrastructure.
Please provide enough details so that I can assess both the feasibility and
the scope. Many folks worry about having to provide their ‘secret sauce’,
but that is never necessary (although you’re perfectly welcome
to send that as well!)</p>
</div>
<div class="section" id="disclaimer">
<h2>Disclaimer<a class="headerlink" href="#disclaimer" title="Permalink to this headline"></a></h2>
<p>The software is provided on the conditions of the simplified BSD license.</p>
<p>This project is not affiliated with Interactive Brokers Group, Inc.’s.</p>
<p>Good luck and enjoy,</p>
<dl class="field-list simple">
<dt class="field-odd">author</dt>
<dd class="field-odd"><p>Ewald de Wit &lt;<a class="reference external" href="mailto:ewald&#46;de&#46;wit&#37;&#52;&#48;gmail&#46;com">ewald<span>&#46;</span>de<span>&#46;</span>wit<span>&#64;</span>gmail<span>&#46;</span>com</a>&gt;</p>
</dd>
</dl>
</div>
</div>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="index.html" class="btn btn-neutral float-left" title="Contents" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="api.html" class="btn btn-neutral float-right" title="API docs" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2021, Ewald de Wit.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>